/*
    Main Module, Contains Initilization, and main program flow
*/

#include "Dark.h"
#include "BJ.h"
#include "Ship.h"
#include "Projectile.h"
#include "Key Manager.h"

Ship *pShip[16];
void ship_thrust_forward
(
    int iShip,
    bool iFlag
)
{
        pShip[iShip]->ThrustForward(iFlag);
}
void ship_thrust_backward
(
    int iShip,
    bool iFlag
)
{
        pShip[iShip]->ThrustBackward(iFlag);
}
void ship_turn_left
(
    int iShip,
    bool iFlag
)
{
        pShip[iShip]->TurnLeft(iFlag);
}
void ship_turn_right
(
    int iShip,
    bool iFlag
)
{
        pShip[iShip]->TurnRight(iFlag);
}
void ship_shoot
(
    int iShip,
    bool iFlag
)
{
        pShip[iShip]->Shoot(iFlag);
}

int tick(void)
{
    KeyManager::refresh();
    ShipManager.for_each(refresh);
    ProjectileManager.for_each(refresh);
    return 0;
}

void DarkGDK(void)
{
    BJ_init();
    Dark dark = Dark();
    DarkBackgroundImage backdrop = DarkBackgroundImage("Backdrop.bmp",1,1);
    DarkImage circleSprt = DarkImage("Circle.bmp",2);
    DarkImage squareSprt = DarkImage("Square.bmp",3);
    DarkImage powerSprt  = DarkImage("Power Bar.bmp",4);
    DarkImage shipSprt   = DarkImage("Fighter Ship Sprite.bmp",5);
    DarkImage turretSprt = DarkImage("Turret Ship Sprite.bmp" ,6);
    //
    //                   Name        ,sprite     ,shield sprt,thrust,friction,turnRate,energyGen,energy,hull,size,mass
    ShipType fighter   ("Fighter"   ,&shipSprt   ,&circleSprt,   450,    0.80,      90,        2,    10,  10,   8,   2);
    ShipType turret    ("Turret"    ,&turretSprt ,&circleSprt,   450,    0.10,      25,        4,    25,  80,  16,  32);
    //
    //                  name         ,sprite     ,velocity,energy, energyRate, energyCap, cooldown,radius,mass,damage
    WeaponType railgun1("Railgun mk1",&circleSprt,  100.0,    1.0,        0.5,         2,        1,   1.0, 0.1,   1.0);
    WeaponType railgun2("Railgun mk2",&circleSprt,  100.0,    2.0,        1.0,         4,        1,   2.0, 0.2,   2.0);
                                               //                       velocity X     Y                
                                               //            start  X     Y              facing    Color          Power bar X
    pShip[0] = ShipManager.create();    pShip[0]->Init(&fighter   ,  600,  400,    0,    0, -90,dbRGB(255,  0,  0),Dark::SIZE_X-16 );
    pShip[1] = ShipManager.create();    pShip[1]->Init(&fighter   ,   40,  400,    0,    0, -90,dbRGB(  0,  0,255),             16 );
    pShip[2] = ShipManager.create();    pShip[2]->Init(&turret    ,  320,  200,    0,    0, -90,dbRGB(  0,255,  0),Dark::SIZE_X/2  );
                      //  type   ,    x,    y,angle
    pShip[0]->addWeapon(&railgun1,    2,    4,    0);
    pShip[0]->addWeapon(&railgun1,    2,   -4,    0);
    pShip[1]->addWeapon(&railgun1,    2,    4,    0);
    pShip[1]->addWeapon(&railgun1,    2,   -4,    0);
    pShip[2]->addWeapon(&railgun2,   14,    0,    0);
    
    KeyManager::register_key(0x26,0,  ship_thrust_forward);//    Up Arrow
    KeyManager::register_key(0x28,0, ship_thrust_backward);//  Down Arrow
    KeyManager::register_key(0x25,0,       ship_turn_left);//  Left Arrow
    KeyManager::register_key(0x27,0,      ship_turn_right);// Right Arrow
    KeyManager::register_key(0xA3,0,           ship_shoot);// Right Control

    KeyManager::register_key('W' ,1,  ship_thrust_forward);// W
    KeyManager::register_key('S' ,1, ship_thrust_backward);// S
    KeyManager::register_key('A' ,1,       ship_turn_left);// A
    KeyManager::register_key('D' ,1,      ship_turn_right);// D
    KeyManager::register_key(0xA2,1,           ship_shoot);// Left Control
    
    KeyManager::register_key('I' ,2,  ship_thrust_forward);// I
    KeyManager::register_key('K' ,2, ship_thrust_backward);// K
    KeyManager::register_key('J' ,2,       ship_turn_left);// J
    KeyManager::register_key('L' ,2,      ship_turn_right);// L
    KeyManager::register_key(' ' ,2,           ship_shoot);// Space Bar

    dark.process(tick);
}